package org.company.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SimpleServlet
 */
public class SimpleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SimpleServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("hello world!,I'm the simplest servlet.");
		//请求的URL路径
		String realUrl = request.getPathInfo();
		//请求的servlet URL地址
		String servletUrl = request.getServletPath();
		String contextPath = request.getContextPath();
		String localAddr = request.getLocalAddr();
		String localName = request.getLocalName();
		int localPort = request.getLocalPort();
		String remoteAddr = request.getRemoteAddr();
		int remotePort = request.getRemotePort();
		String remoteName = request.getRemoteHost();
		String remoteUser = request.getRemoteUser();
		
		request.setAttribute("realUrl", realUrl);
		request.setAttribute("servletUrl",servletUrl);
		request.setAttribute("contextPath", contextPath);
		request.setAttribute("localAddr", localAddr);
		request.setAttribute("localName", localName);
		request.setAttribute("localPort", localPort);
		request.setAttribute("remoteAddr", remoteAddr);
		request.setAttribute("remotePort", remotePort);
		request.setAttribute("remoteName", remoteName);
		request.setAttribute("remoteUser", remoteUser);
		//request.getRequestDispatcher("/npage/demo/servletInfo.jsp").forward(request, response);
		//request中的参数无法传递给response的方式返回数据
		PrintWriter out = response.getWriter();
		out.write("contextPath=" + contextPath);
		response.sendRedirect(request.getContextPath() + "/npage/demo/servletInfo.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
